﻿using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace PerfAssist.Misc
{
    public class TopNContainer
    {
        public const int MaxCount = 6;

        private readonly StringBuilder m_strBuilder = new StringBuilder(256);

        public List<KeyValuePair<double, string>> TopN = new List<KeyValuePair<double, string>>(MaxCount);

        public bool TryAdd(double value, string text)
        {
            if (TopN.Count == MaxCount && value <= TopN[TopN.Count - 1].Key) return false;

            var inserted = false;
            for (var i = 0; i < TopN.Count; ++i)
                if (value > TopN[i].Key)
                {
                    TopN.Insert(i, new KeyValuePair<double, string>(value, text));
                    inserted = true;
                    break;
                }

            if (!inserted)
            {
                if (TopN.Count < MaxCount)
                {
                    TopN.Add(new KeyValuePair<double, string>(value, text));
                    return true;
                }

                Debug.LogWarningFormat("[TopNContainer] (大于下限却找不到插入点)。(len: {0}, lowest: {1}, value: {2})",
                    TopN.Count, TopN[TopN.Count - 1].Key, value);
                return false;
            }

            while (TopN.Count > MaxCount) TopN.RemoveAt(TopN.Count - 1);

            return true;
        }

        public string ItemToString(int i)
        {
            m_strBuilder.Length = 0;
            return m_strBuilder.AppendFormat("({0:0.00}) {1}", TopN[i].Key, TopN[i].Value).ToString();
        }

        public void Clear()
        {
            TopN.Clear();
        }
    }


    public class ScreenLogger : MonoBehaviour
    {
        public enum LogAnchor
        {
            TopLeft,
            TopRight,
            BottomLeft,
            BottomRight
        }

        public static bool IsPersistent = true;

        private static ScreenLogger instance;

        private static Queue<LogMessage> queue = new Queue<LogMessage>();

        public LogAnchor AnchorPosition = LogAnchor.BottomLeft;
        public TopNContainer AsyncTopN = new TopNContainer();
        public Color BackgroundColor = Color.black;

        [Range(0f, 01f)] public float BackgroundOpacity = 0.5f;

        private bool destroying;
        public Color ErrorColor = new Color(1, 0.5f, 0.5f);

        public int FontSize = 16;


        [Tooltip("Height of the log area as a percentage of the screen height")] [Range(0.3f, 1.0f)]
        public float Height = 0.7f;

        public bool LogErrors = true;

        public bool LogMessages = true;
        public bool LogWarnings = true;

        public int Margin = 20;

        public Color MessageColor = Color.white;
        private readonly int padding = 20;
        public bool ShowInEditor = true;

        public bool ShowLog = true;
        public bool StackTraceErrors = true;

        public bool StackTraceMessages;
        public bool StackTraceWarnings;

        private GUIStyle styleContainer, styleText;

        public TopNContainer SyncTopN = new TopNContainer();

        public Dictionary<string, Color> TagColors = new Dictionary<string, Color>
        {
            {"#LuaCache", Color.green},
            {"#LuaIO", Color.red}
        };

        public Color WarningColor = Color.yellow;

        [Tooltip("Width of the log area as a percentage of the screen width")] [Range(0.3f, 1.0f)]
        public float Width = 0.7f;

        public static bool Instantiated { get; private set; }

        public static ScreenLogger Instance
        {
            get
            {
                if (Instantiated) return instance;

                instance = FindObjectOfType(typeof(ScreenLogger)) as ScreenLogger;

                // Object not found, we create a new one
                if (instance == null)
                {
                    //// Try to load the default prefab
                    //try
                    //{
                    //    instance = Instantiate(Resources.Load("ScreenLoggerPrefab", typeof(ScreenLogger))) as ScreenLogger;
                    //}
                    //catch (Exception e)
                    //{
                    //    Debug.Log("Failed to load default Screen Logger prefab...");
                    //}
                    instance = new GameObject("ScreenLogger", typeof(ScreenLogger)).GetComponent<ScreenLogger>();

                    // Problem during the creation, this should not happen
                    if (instance == null)
                        Debug.LogError("Problem during the creation of ScreenLogger");
                    else Instantiated = true;
                }
                else
                {
                    Instantiated = true;
                }

                return instance;
            }
        }

        public void Awake()
        {
            var obj = FindObjectsOfType<ScreenLogger>();

            if (obj.Length > 1)
            {
                Debug.Log("Destroying ScreenLogger, already exists...");

                destroying = true;

                Destroy(gameObject);
                return;
            }

            InitStyles();

            if (IsPersistent)
                DontDestroyOnLoad(this);
        }

        private void InitStyles()
        {
            var back = new Texture2D(1, 1);
            BackgroundColor.a = BackgroundOpacity;
            back.SetPixel(0, 0, BackgroundColor);
            back.Apply();

            styleContainer = new GUIStyle();
            styleContainer.normal.background = back;
            styleContainer.wordWrap = true;
            styleContainer.padding = new RectOffset(padding, padding, padding, padding);

            styleText = new GUIStyle();
            styleText.fontSize = FontSize;
        }

        private void OnEnable()
        {
            if (!ShowInEditor && Application.isEditor) return;

            queue = new Queue<LogMessage>();

#if UNITY_4_5 || UNITY_4_6 || UNITY_4_7
            Application.RegisterLogCallback(HandleLog);
#else
            Application.logMessageReceived += HandleLog;
#endif
        }

        private void OnDisable()
        {
            // If destroyed because already exists, don't need to de-register callback
            if (destroying) return;

#if UNITY_4_5 || UNITY_4_6 || UNITY_4_7
            Application.RegisterLogCallback(null);
#else
            Application.logMessageReceived -= HandleLog;
#endif
        }

        private void Update()
        {
            if (!ShowInEditor && Application.isEditor) return;

            var InnerHeight = (Screen.height - 2 * Margin) * Height - 2 * padding;
            var TotalRows = (int) (InnerHeight / styleText.lineHeight);

            // Remove overflowing rows
            while (queue.Count > TotalRows)
                queue.Dequeue();
        }


        private void OnGUI()
        {
            if (!ShowLog) return;
            if (!ShowInEditor && Application.isEditor) return;

            var w = (Screen.width - 2 * Margin) * Width;
            var h = (Screen.height - 2 * Margin) * Height;
            float x = 1, y = 1;

            switch (AnchorPosition)
            {
                case LogAnchor.BottomLeft:
                    x = Margin;
                    y = Margin + (Screen.height - 2 * Margin) * (1 - Height);
                    break;

                case LogAnchor.BottomRight:
                    x = Margin + (Screen.width - 2 * Margin) * (1 - Width);
                    y = Margin + (Screen.height - 2 * Margin) * (1 - Height);
                    break;

                case LogAnchor.TopLeft:
                    x = Margin;
                    y = Margin;
                    break;

                case LogAnchor.TopRight:
                    x = Margin + (Screen.width - 2 * Margin) * (1 - Width);
                    y = Margin;
                    break;
            }

            GUILayout.BeginArea(new Rect(x, y, w, h), styleContainer);

            foreach (var m in queue)
            {
                switch (m.Type)
                {
                    case LogType.Warning:
                        styleText.normal.textColor = WarningColor;
                        break;

                    case LogType.Log:
                        styleText.normal.textColor = MessageColor;
                        break;

                    case LogType.Assert:
                    case LogType.Exception:
                    case LogType.Error:
                        styleText.normal.textColor = ErrorColor;
                        break;

                    default:
                        styleText.normal.textColor = MessageColor;
                        break;
                }

                if (m.Message.Contains("#"))
                    foreach (var p in TagColors)
                        if (m.Message.Contains(p.Key))
                        {
                            styleText.normal.textColor = p.Value;
                            break;
                        }

                GUILayout.Label(m.Message, styleText);
            }

            GUILayout.EndArea();

            var topNWidth = 550.0f;
            var topNHeight = (Screen.height - 2 * Margin - Margin) * (1.0f - Height);
            styleText.normal.textColor = Color.white;

            GUILayout.BeginArea(new Rect(Margin, Margin, topNWidth, topNHeight), styleContainer);
            GUILayout.Label("Sync Top N", styleText);
            for (var i = 0; i < SyncTopN.TopN.Count; ++i)
                GUILayout.Label(SyncTopN.ItemToString(i), styleText);
            GUILayout.EndArea();

            GUILayout.BeginArea(new Rect(Margin * 2 + topNWidth, Margin, topNWidth, topNHeight), styleContainer);
            GUILayout.Label("Async Top N", styleText);
            for (var i = 0; i < AsyncTopN.TopN.Count; ++i)
                GUILayout.Label(AsyncTopN.ItemToString(i), styleText);
            GUILayout.EndArea();
        }

        public void Clear()
        {
            SyncTopN.Clear();
            AsyncTopN.Clear();
        }

        private void HandleLog(string message, string stackTrace, LogType type)
        {
            if (type == LogType.Assert && !LogErrors) return;
            if (type == LogType.Error && !LogErrors) return;
            if (type == LogType.Exception && !LogErrors) return;
            if (type == LogType.Log && !LogMessages) return;
            if (type == LogType.Warning && !LogWarnings) return;

            var lines = message.Split('\n');

            foreach (var l in lines)
                queue.Enqueue(new LogMessage(l, type));

            if (type == LogType.Assert && !StackTraceErrors) return;
            if (type == LogType.Error && !StackTraceErrors) return;
            if (type == LogType.Exception && !StackTraceErrors) return;
            if (type == LogType.Log && !StackTraceMessages) return;
            if (type == LogType.Warning && !StackTraceWarnings) return;

            var trace = stackTrace.Split('\n');

            foreach (var t in trace)
                if (t.Length != 0)
                    queue.Enqueue(new LogMessage("  " + t, type));
        }

        //StringBuilder m_strBuilder = new StringBuilder(256);

        //string GetStackFrame(int skipFrames)
        //{
        //    var frame = new StackFrame(skipFrames, true);
        //    if (frame == null || frame.GetMethod() == null)
        //        return ""; 

        //    m_strBuilder.Length = 0;
        //    m_strBuilder.AppendFormat("    {0}() - {1}", frame.GetMethod().Name, frame.GetFileName());
        //    return m_strBuilder.ToString();
        //}

        public void EnqueueDirectly(string message, LogType type)
        {
            queue.Enqueue(new LogMessage(message, type));
            //queue.Enqueue(new LogMessage(GetStackFrame(10), type));
        }

        public void InspectorGUIUpdated()
        {
            InitStyles();
        }

        private class LogMessage
        {
            public readonly string Message;
            public readonly LogType Type;

            public LogMessage(string msg, LogType type)
            {
                Message = msg;
                Type = type;
            }
        }
    }
}

/*
The MIT License

Copyright © 2016 Screen Logger - Giuseppe Portelli <giuseppe@aclockworkberry.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/